<template>
  <el-dialog v-el-drag-dialog :title="!form.deptId?'新建机构':'编辑机构'" :visible.sync="visible" :append-to-body="appendToBody" :before-close="onClose">
    <el-form ref="deptForm" :model="form" :rules="rules" label-width="120px" :disabled="!hasLoaded">
      <el-form-item hidden>
        <el-input v-model="form.deptId" hidden />
      </el-form-item>
      <el-form-item prop="parentId" hidden>
        <el-input v-model="form.parentId" hidden />
      </el-form-item>
      <el-form-item prop="ancestors" hidden>
        <el-input v-model="form.ancestors" hidden />
      </el-form-item>
      <el-form-item prop="leader" hidden>
        <el-input v-model="form.leader" hidden />
      </el-form-item>

      <el-card>
        <el-form-item label="上级部门" prop="parentName">
          <el-input v-model="form.parentName" readonly>
            <el-button v-if="hasLoaded" slot="append" icon="el-icon-search" @click="selectDept" />
          </el-input>
        </el-form-item>
        <el-form-item label="部门名称" prop="deptName">
          <el-input v-model="form.deptName" />
        </el-form-item>
        <el-form-item label="显示排序" prop="orderNum">
          <el-input-number v-model="form.orderNum" :min="1" />
        </el-form-item>
        <el-form-item label="负责人" prop="leaderName">
          <el-input v-model="form.leaderName" readonly>
            <el-button v-if="hasLoaded" slot="append" icon="el-icon-search" @click="selectUser" />
          </el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status">
            <el-option
              v-for="item in optionOfStatus"
              :key="item.dictCode"
              :label="item.dictLabel"
              :value="item.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            :rows="3"
          />
        </el-form-item>
      </el-card>
    </el-form>

    <dept-tree-dialog v-if="openDeptDialog" ref="deptDialog" @confirmDialog="setDept" />
    <user-table-dialog v-if="openUserDialog" ref="userDialog" :single="true" @confirmDialog="setUser" />
    <span slot="footer" class="dialog-footer">
      <el-button v-if="hasLoaded" type="primary" icon="el-icon-check" @click="onSubmit">确 定</el-button>
      <el-button icon="el-icon-close" @click="onClose">关 闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { deptLoad, deptSave } from '@/api/system/dept'
import DeptTreeDialog from '@/views/system/dept/tree/dialog'
import UserTableDialog from '@/views/system/user/table/dialog'

export default {
  name: 'DeptFormDialog',
  components: {
    DeptTreeDialog,
    UserTableDialog
  },
  data() {
    return {
      visible: false,
      appendToBody: false,
      hasLoaded: false,
      form: {
        deptId: undefined,
        parentId: undefined,
        ancestors: undefined,
        parentName: '',
        deptName: '',
        orderNum: 1,
        leader: undefined,
        leaderName: undefined,
        status: undefined,
        phone: '',
        email: '',
        remark: ''
      },
      rules: {
        parentName: [{ required: true, message: '请选择上级部门' }],
        deptName: [{ required: true, message: '请填写部门名称' }],
        leaderName: [{ required: true, message: '请选择负责人' }],
        email: [{ type: 'email', message: '邮箱格式不正确' }]
      },
      optionOfStatus: [],
      openDeptDialog: false,
      openUserDialog: false
    }
  },
  methods: {
    init(deptId, parentId) {
      this.hasLoaded = false
      this.form.deptId = deptId || null
      this.form.parentId = parentId || null
      this.visible = true
      this.$nextTick(() => {
        this.loadForm(deptId, parentId)
        this.getDictData()
      })
    },
    loadForm(deptId, parentId) {
      deptLoad({
        'id': deptId,
        'pid': parentId
      }).then(response => {
        this.form = this.$api.objectUpdate(this.form, response.param)
        // 判断当前用户权限
        this.hasLoaded = this.$perm.hasPermissionOr(['system:dept:edit'])
      }).catch(() => {})
    },
    getDictData() {
      this.$dictGroup('sys_normal_disable').then(response => {
        this.optionOfStatus = response
      }).catch(e => {})
    },
    selectDept() {
      this.openDeptDialog = true
      const selected = this.form.parentId
      this.$nextTick(() => {
        this.$refs.deptDialog.init(selected)
      })
    },
    setDept(node) {
      this.form.parentId = node[0].id
      this.form.parentName = node[0].title
    },
    selectUser() {
      this.openUserDialog = true
      this.$nextTick(() => {
        this.$refs.userDialog.init()
      })
    },
    setUser(row) {
      this.form.leader = row.map(value => value.userId).join(',')
      this.form.leaderName = row.map(value => value.userName).join(',')
    },
    onSubmit() {
      this.$refs.deptForm.validate(valid => {
        if (valid) {
          deptSave(this.form).then(response => {
            this.$modal.msgSuccess(response.msg)
            this.$emit('confirmForm')
            this.onClose()
          }).catch(() => {})
        }
      })
    },
    onClose() {
      this.$refs.deptForm.resetFields()
      this.visible = false
    }
  }
}
</script>
